﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataReceiver
{
    public partial class FormConnections : Form
    {
        public List<IpEndpoint> IPs
        {
            get
            {
                var list = from object o in listBoxIPAddress.Items
                           select o as IpEndpoint;

                return list.ToList();
            }
            set
            {
                foreach (var l in value)
                {
                    listBoxIPAddress.Items.Add(l);
                }
            }
        }

        public FormConnections()
        {
            InitializeComponent();
        }

        private void listBoxIPAddress_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selected = listBoxIPAddress.SelectedItem as IpEndpoint;
            if (selected != null)
            {
                textBoxIp.Text = selected.ToString();

            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            IpEndpoint ip;
            if (Helper.TryParse(textBoxIp.Text, out ip))
            {
                listBoxIPAddress.Items.Add(ip);
            }
            else
            {
                MessageBox.Show("请按正确格式输入，格式为IP:端口号");
            }
        }

        
        private void buttonDel_Click(object sender, EventArgs e)
        {
            var selected = listBoxIPAddress.SelectedItem;
            if (selected != null)
            {
                listBoxIPAddress.Items.Remove(selected);
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}
